
/** 简单柱状图(只有柱子) */
const defaultSimpleBarOption = {
  tooltip: {
    trigger: 'axis',
    axisPointer: { type: 'none' }
  },
  xAxis: {
    type: 'category',
    data: [],
    axisTick: { show: false },
    axisLine: { show: false },
    axisLabel: { show: false }
  },
  yAxis: {
    type: 'value',
    axisTick: { show: false },
    axisLine: { show: false },
    axisLabel: { show: false },
    splitLine: { show: false }
  },
  series: [],
  dataset: {
    source: []
  }
}

export function getSimpleBarOption(title = '', source = [], type = 'bar') {
  const option = defaultSimpleBarOption
  option.title.text = title
  option.dataset.source = source
  if (source.length > 0) {
    option.series = source[0].slice(1).map(s => {
      return {
        type: type
      }
    })
  }

  return option
}

/** 仪表盘 */
export const defaultGaugeOption = {
  title: {
    text: ''
  },
  legend: { show: false, orient: 'vertical', right: '5%', top: '15%', icon: 'circle' },
  tooltip: {},
  series: [
    {
      name: '业务指标',
      type: 'gauge',
      detail: { formatter: '{value}' },
      data: []
    }
  ]
}

export function getGaugeOption(title = '', source = [], type = 'gauge') {
  const option = defaultGaugeOption

  if (source.length > 0) {
    option.series[0].data = source.map(value => {
      return {
        name: value[0],
        value: value[1]
      }
    })
  }

  option.title.text = title
  return option
}

/** 雷达图 */
export const defaultRadarOption = {
  title: {
    text: ''
  },
  legend: {
    data: []
  },
  tooltip: {},
  radar: {
    // shape: 'circle',
    name: {
      textStyle: {
        color: '#fff',
        backgroundColor: '#999',
        borderRadius: 3,
        padding: [3, 5]
      }
    },
    indicator: []
  },
  series: [{
    name: '',
    type: 'radar',
    // areaStyle: {normal: {}},
    data: []
  }]
}

export function getRadarOption(title = '', source = [], type = 'radar') {
  const option = defaultRadarOption

  if (source.length > 0) {
    // 雷达图 indicator
    option.radar.indicator = source[0].slice(1).map((value, index) => {
      return {
        name: value,
        max: source[1][index + 1]
      }
    })

    // 雷达图 data
    for (let i = 2; i < source.length; i++) {
      var item = {}
      item.name = source[i][0]
      item.value = source[i].slice(1)
      option.series[0].data.push(item)
    }
  }

  option.title.text = title
  return option
}

/** 甜甜圈 */
export const defaultDoughnutOption = {
  title: {
    text: ''
  },
  tooltip: {
    trigger: 'item',
    formatter: '{a} <br/>{b}: ({d}%)'
  },
  legend: {
    show: false,
    orient: 'vertical',
    right: 10
    // data: ['直接访问', '邮件营销', '联盟广告', '视频广告', '搜索引擎']
  },
  series: [
    {
      name: '',
      type: 'pie',
      radius: ['50%', '70%'],
      avoidLabelOverlap: false,
      label: {
        show: true
      }
    }
  ],
  dataset: {
    source: []
  }
}

export function getDoughnutOption(title = '', source = [], type = 'bar') {
  const option = defaultDoughnutOption
  option.title.text = title
  option.dataset.source = source
  return option
}

export const defaultPieOption = {
  title: {
    text: ''
  },
  legend: { show: false, orient: 'vertical', right: '5%', top: '15%', icon: 'circle' },
  tooltip: {},
  series: [
  ],
  dataset: {
    source: []
  }
}

export function getPieOption(title = '', source = [], type = 'bar') {
  const option = defaultPieOption
  option.title.text = title
  option.dataset.source = source
  if (source.length > 0) {
    option.series = source[0].slice(1).map(s => {
      return {
        type: type
      }
    })
  }

  return option
}

const defaultBarOption = {
  title: {
    text: ''
  },
  tooltip: {},
  legend: { show: false, left: 0, top: 0 },
  toolbox: { show: true, feature: { magicType: { show: true, type: ['bar', 'line'] }, restore: { show: true }}},
  grid: { left: '5%', right: '5%', top: '15%', bottom: 0, containLabel: true },
  xAxis: { type: 'category' },
  yAxis: { type: 'value' },
  series: [],
  dataset: {
    source: []
  }
}

export function getBarOption(title = '', source = [], type = 'bar') {
  const option = defaultBarOption
  option.title.text = title
  option.dataset.source = source
  if (source.length > 0) {
    option.series = source[0].slice(1).map(s => {
      return {
        type: type
      }
    })
  }

  return option
}
